﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class kjxy_index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            TreasurerDataContext db = new TreasurerDataContext();
            //
            //学院简介
            //
            //string brief_id = GetFourmId("学院简介");
            //var brief = from aA in db.Articles where aA.ForumID.Equals(brief_id) select aA;
            //Repeater1.DataSource = brief;
            //Repeater1.DataBind();

            //
            //工作动态
            //
            string work_id = GetFourmId("工作动态");
            var work = (from aA in db.Articles where aA.ForumID.Equals(work_id) orderby aA.Published descending select aA).Take(10);
            Repeater2.DataSource = work;
            Repeater2.DataBind();
            //
            //图片新闻
            //
            string pic_id = GetFourmId("图片新闻");
            var pic = from aA in db.Articles where aA.ForumID.Equals(pic_id) orderby aA.Published descending select aA;
            DataList1.DataSource = pic;
            DataList1.DataBind();
            //Image1.ImageUrl = GetImageUrl(pic.First().Body); 
            
            //
            //教学科研
            //
            string research_id = GetFourmId("教学科研");
            var research = (from aA in db.Articles where aA.ForumID.Equals(research_id) orderby aA.Published descending select aA).Take(5);
            Repeater3.DataSource = research;
            Repeater3.DataBind();
            //
            //招生就业
            //
            string work2_id = GetFourmId("招生就业");
            var work2 = (from aA in db.Articles where aA.ForumID.Equals(work2_id) orderby aA.Published descending select aA).Take(5);
            Repeater4.DataSource = work2;
            Repeater4.DataBind();
            //
            //学生工作
            //
            string action_id = GetFourmId("学生工作");
            var action = (from aA in db.Articles where aA.ForumID.Equals(action_id) orderby aA.Published descending select aA).Take(5);
            Repeater5.DataSource = action;
            Repeater5.DataBind();
            //
            //图片
            //
            string pic2_id = GetFourmId("图片");
            var pic2 = from aA in db.Articles where aA.ForumID.Equals(pic2_id) orderby aA.Published descending select aA;
            DataList2.DataSource = pic2;
            DataList2.DataBind();
            //Image1.ImageUrl = GetImageUrl(pic.First().Body); 
            
        }
    }
    public string GetImageUrl(string content)
    {
        Regex regex = new Regex(@"<IMG[^>]+src=\s*(?:'(?<src>[^']+)'|""(?<src>[^""]+)""|(?<src>[^>\s]+))\s*[^>]*>", RegexOptions.IgnoreCase);
        MatchCollection mc = regex.Matches(content);
        if (mc.Count != 0)
        {
            return mc[0].Groups["src"].Value.ToLower();
        }
        else
        {
            return "";
        }

    }
    public string GetFourmId(string name)
    {
        TreasurerDataContext db = new TreasurerDataContext();
        string fourm_id = (from aF in db.Forums where aF.Name.Equals(name) select aF.ID).First();
        return fourm_id;
    }
    public string GetArticle_id(string forum_name)
    {
        TreasurerDataContext db = new TreasurerDataContext();
        string forum_id = GetFourmId(forum_name);
        string article_id = (from aA in db.Articles where aA.ForumID.Equals(forum_id) select aA.ID).First();
        return article_id;
    }
}